package com.itheima.service;

import com.itheima.dao.ContactMapper;
import com.itheima.pojo.Contact;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class ContactService {
    public List<Contact> findAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        List<Contact> list = mapper.findAll();
        MybatisUtils.close(sqlSession);
        return list;
    }

    public boolean deleteContactById(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        int i = mapper.deleteContactById(id);
        MybatisUtils.close(sqlSession);
        return i > 0;
    }

    public boolean addContact(Contact contact) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        int i = mapper.addContact(contact);
        MybatisUtils.close(sqlSession);
        return i > 0;
    }

    public Contact findContactById(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        Contact contact = mapper.findContactById(id);
        MybatisUtils.close(sqlSession);
        return contact;
    }

    public boolean updateContactById(Contact contact) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        int i = mapper.updateContact(contact);
        MybatisUtils.close(sqlSession);
        return i > 0;
    }

    public void deleteMulti(String[] ids) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        for (String id : ids) {
            mapper.deleteContactById(id);
        }
        MybatisUtils.close(sqlSession);
    }

    public List<Contact> findByCondition(Map<String, String> map) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        List<Contact> list = mapper.findByCondition(map);
        MybatisUtils.close(sqlSession);
        return list;
    }

    public int findTotalCount() {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        int totalCount = mapper.findTotalCount();
        MybatisUtils.close(sqlSession);
        return totalCount;
    }

    public List<Contact> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        List<Contact> list = mapper.findByPage((currentPage - 1) * pageSize, pageSize);
        MybatisUtils.close(sqlSession);
        return list;
    }
}
